/*
 * Copyright 2014 Rivaldo.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hiva.gmovie.reader;

import com.hiva.gmovie.domain.Movie;
import com.hiva.gmovie.domain.Showtime;
import com.hiva.gmovie.util.Constants;
import com.hiva.gmovie.util.ExtractorHelper;
import java.util.ArrayList;
import java.util.List;
import net.htmlparser.jericho.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Utility class to read the movie showtime information.
 * @author Rivaldo
 */
public class ShowtimeReader {
    
    private static final Logger LOG = LoggerFactory.getLogger(ShowtimeReader.class);
    
    public List<Showtime> readShowtime(Element showtimeTag) {
        
        List<Showtime> showtimeList = new ArrayList<Showtime>();
        List<Element> moviesTag = showtimeTag.getAllElements(Constants.SHOWTIME_MOVIE);
        
        for (Element movie : moviesTag) {
            showtimeList.add(getShowtime(movie));
        }
        
        return showtimeList;
    }
    
    private Movie getMovie(Element movieTag) {
        
        LOG.trace("Looking for movie name and information...");
        String name = ExtractorHelper.getTagText(movieTag, Constants.SHOWTIME_MOVIE_NAME);
        String info = ExtractorHelper.getTagText(movieTag, Constants.SHOWTIME_MOVIE_INFO);
        
        return new Movie(name, info);
    }
    
    private Showtime getShowtime(Element movieTag) {
        
        Movie movie = getMovie(movieTag);
        LOG.trace("Looking for movie times information...");
        
        String times = ExtractorHelper.getTagText(movieTag, Constants.SHOWTIME_MOVIE_TIMES);
        //times = times.replace(' ', '-');
        
        return new Showtime(movie, ExtractorHelper.splitData(times));
    }

}
